package com.webmall.servlet.user;

import com.webmall.entity.UserEntity;
import com.webmall.service.webmall_UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserLogin")
public class UserLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");

        UserEntity user = webmall_UserDao.selectById(id);

        String true_pwd = user.getUSER_PWD();
        System.out.println(true_pwd);

        if (pwd.equals(true_pwd)){
            HttpSession session = request.getSession();
            session.setAttribute("login_user",user);//把登录的user保存进session
            session.setAttribute("isLogin","1");//把登录标志位也保存进session

            response.sendRedirect("/indexSelect");//成功

        }else {
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户名或密码错误');");
            out.write("location.href='login.jsp';");
            out.write("</script>");
        }

    }


}
